package com.idrive.activity;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.idrive.R;
import com.idrive.database.dao.LocationDataDao;
import com.idrive.database.dto.LocationData;
import com.idrive.service.PathTrackerLocationService;

public class PathTrackerViewActivity extends MapActivity {

	private List<Overlay> mapOverlays;
	private MapController mapController;
	private MapView mapView;
	private GeoPoint gP;
	private GmapOverlay gmapOverlay;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (!isPathTrackerLocationRunning()) {
			startService(new Intent(PathTrackerViewActivity.this, PathTrackerLocationService.class));
		}

		setContentView(R.layout.activity_path_tracker_view);

		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		if (LocationData.CURRENT_LOCATION != null) {
			gP = new GeoPoint((int) (LocationData.CURRENT_LOCATION.getLatitude() * 1E6),
			        (int) (LocationData.CURRENT_LOCATION.getLongitude() * 1E6));
		} else {
			gP = new GeoPoint(6763232, 80123123); // Creating center GeoPoint
		}

		mapController = mapView.getController(); // Initializing the MapController and setting the map to center at the
		                                         // defined GeoPoint
		mapController.setCenter(gP);
		mapController.setZoom(9);

		mapOverlays = mapView.getOverlays();
		gmapOverlay = new GmapOverlay(this);
		mapOverlays.add(gmapOverlay);

	}

	@Override
	protected boolean isRouteDisplayed() {
		return true;
	}

	private boolean isPathTrackerLocationRunning() {
		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
			if ("com.idrive.service.PathTrackerLocationService".equals(service.service.getClassName())) {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}

	class GmapOverlay extends Overlay {

		private Context context;

		public GmapOverlay(Context context) {
			this.context = context;
		}

		@Override
		public void draw(Canvas canvas, MapView mapv, boolean shadow) {
			super.draw(canvas, mapv, shadow);

			LocationDataDao dao = new LocationDataDao(context);

			List<LocationData> groupList = dao.getGroupLocation();

			for (LocationData group : groupList) {

				// Configuring the paint brush
				Paint paint = new Paint();
				paint.setDither(true);
				paint.setColor(Color.RED);
				paint.setStyle(Paint.Style.FILL_AND_STROKE);
				paint.setStrokeJoin(Paint.Join.ROUND);
				paint.setStrokeCap(Paint.Cap.ROUND);
				paint.setStrokeWidth(2);

				List<LocationData> list = dao.getLocation(group.getGroupId());

				boolean first = true;
				Point prvPoint = null;
				for (LocationData data : list) {
					GeoPoint geoPoint = new GeoPoint((int) (data.getLatitude() * 1E6),
					        (int) (data.getLongitude() * 1E6));
					Point point = new Point();
					mapView.getProjection().toPixels(geoPoint, point);

					if (first) {
						first = false;
					} else {
						canvas.drawLine(prvPoint.x, prvPoint.y, point.x, point.y, paint);
					}

					prvPoint = point;
				}
			}

			if (LocationData.CURRENT_LOCATION != null) {
				// Configuring the paint brush
				Paint paint = new Paint();
				paint.setDither(true);
				paint.setColor(Color.BLUE);
				paint.setStyle(Paint.Style.FILL_AND_STROKE);
				paint.setStrokeJoin(Paint.Join.ROUND);
				paint.setStrokeCap(Paint.Cap.ROUND);
				paint.setStrokeWidth(2);

				GeoPoint geoPoint = new GeoPoint((int) (LocationData.CURRENT_LOCATION.getLatitude() * 1E6),
				        (int) (LocationData.CURRENT_LOCATION.getLongitude() * 1E6));
				Point point = new Point();
				mapView.getProjection().toPixels(geoPoint, point);

				canvas.drawCircle(point.x, point.y, 1, paint);
			}

		}

	}
}
